package com.dxp.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxp.common.R;
import com.dxp.pojo.Employee;
import com.dxp.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    //登录
    @RequestMapping("/login")
    public R<Employee>  login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        QueryWrapper<Employee> wrapper = new QueryWrapper<Employee>();
        wrapper.eq("username",employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        if (emp == null){
            return R.error("登录失败");
        }
        if(emp.getStatus()==0){
            return R.error("用户已禁用");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("登陆失败");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    //注销
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("注销成功");
    }
    //新增员工
    @PostMapping
    public R<String> addEmp(HttpServletRequest request,@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(employee1);
        employee.setUpdateUser(employee1);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name) {
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<Employee>();
        queryWrapper.like(StringUtils.isNotEmpty(name),"name",name);
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    //修改信息
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(employee1);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> updateInfo(@PathVariable Long id){
        log.info("获取到id:"+id);
        Employee employeeServiceById = employeeService.getById(id);
        if(employeeServiceById!=null){
            return R.success(employeeServiceById);
        }
        return R.error("没有查询到结果");
    }
}
